package fr.umlv.ig.infoView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.umlv.ig.extraWindows.StationWindow;
import fr.umlv.ig.metroApplication.InfoPanel;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;

@SuppressWarnings("serial")

/**
 * this class is used for showing informations about a station
 */
public class StationInfoView extends JPanel {
	
	/**
	 * model use of collect information
	 */
	private final MetroModelImpl metroModel;
	
	/**
	 * station that the infoview is showing
	 */
	private Station currentStation;
	
	private static final String stationString =  new String("Station : ");
	private static final String lineString =  new String("Line : ");
	private static final String stopTime =  new String("Stop time : ");
	private static final String coordinate =  new String("Coordinate : ");
	private static final String stateOpen =  new String("State : Open");
	private static final String stateClose =  new String("State : Close");
	
	private final JLabel labelName = new JLabel(stationString);
	private final JLabel lineNumber = new JLabel(lineString);
	private final JLabel labelWaitingTime = new JLabel(stopTime);
	private final JLabel labelCoordinate = new JLabel(coordinate);
	private final JLabel labelIsOpened = new JLabel("State : ");
	private final JButton buttonConfig = new JButton("Station Configuration");
	
	/**
	 * window for buttonConfig button
	 */
	private StationWindow config;
	
	/**
	 * initialize the StationInfoView
	 * @param frame frame for create configuration window
	 * @param metroModel model use for getting informations
	 */
	public StationInfoView(final JFrame frame, final MetroModelImpl metroModel) {
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(Box.createVerticalStrut(10));
		add(labelName);
		add(Box.createVerticalStrut(7));
		add(lineNumber);
		add(Box.createVerticalStrut(7));
		add(labelWaitingTime);
		add(Box.createVerticalStrut(7));
		add(labelCoordinate);
		add(Box.createVerticalStrut(7));
		add(labelIsOpened);
		add(Box.createVerticalStrut(7));
		add(buttonConfig);
		buttonConfig.addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
				if(currentStation == null) {
					return;
				}
				if(config == null) {
					config = new StationWindow(frame, "Station configuration", metroModel, currentStation);
				}
				else {
					config.reload(currentStation);
				}
				config.start();	
			}
		});
		this.metroModel = metroModel;
	}
	
	/**
	 * set the info
	 * @param l station that will be use by model for getting information
	 */
	public void setInfo(final Station station, final JFrame frame) {
		currentStation = station;
		labelName.setText(stationString + station.getName());
		lineNumber.setText(lineString + metroModel.getLine(station).getNumber());
		labelWaitingTime.setText(stopTime+metroModel.getWaitingTime(station));
		
		
		String coordinateString = InfoPanel.pointToString(metroModel.getCoordinate(station));
		
		labelCoordinate.setText(coordinateString);
		if(metroModel.isOpened(station)) {
			labelIsOpened.setText(stateOpen);
		}
		else {
			labelIsOpened.setText(stateClose);
		}		
		repaint();
	}

}